from selenium.webdriver.common.by import By

from page_object.pages.BasePage import BasePage


class ChatRoom(BasePage):
    _img_gift = (By.ID, 'img_gift')   #送礼物按钮
    _tv_msg_user_name = (By.ID, 'tv_msg_user_name')
    _ll_sendgift = (By.ID, 'll_sendgift')
    _tv_ds = (By.ID, 'tv_ds')
    _img_menu = (By.ID, 'img_menu')

    def send_gift(self, key):
        '''
        送礼物
        :param key:礼物名称，暂时只支持第一页的礼物名称
        :return:
        '''

        gift_name1 = (By.XPATH,
                       "//*[contains(@resource-id, 'tv_name') and contains(@text, '%s')]/../.." % key +
                       "//*[contains(@resource-id, 'img_gift')]")

        self.find(self._tv_msg_user_name).click()
        self.find(self._ll_sendgift).click()
        self.find(gift_name1).click()
        self.find(self._tv_ds).click()
        return self

    def send_red_rain(self, time, mun, money, remarks):
        '''
        发红包雨
        :param time:设置几秒后下红包雨，需要是列表里有的时间
        :param mun:发送红包个数
        :param money:发多少钱
        :param remarks:备注
        :return:
        '''
        self.find(self._img_menu).click()
        self.find_by_xpath('发红包').click()
        self.find_by_xpath('请选择').click()
        self.find_by_xpath('%s秒'%time).click()
        self.find_by_xpath('最少发1个红包').send_keys(mun)
        self.find_by_xpath('最低1').send_keys(money)
        self.find_by_xpath('随意玩乐，我买单～').send_keys(remarks)
        self.find_by_xpath('发红包').click()
        return self

    def send_coin_red(self, mun, money, remarks):
        '''
        发金币红包
        :param mun:红包个数
        :param money:红包钱数
        :param remarks:备注
        :return:
        '''
        red_money = (By.ID, 'et_red_money')
        cb_only_mic = (By.ID, 'cb_only_mic')
        self.find(self._img_menu).click()
        self.find_by_xpath('发红包').click()
        self.find_by_xpath('金币红包').click()
        self.find_by_xpath('填写个数').send_keys(mun)
        self.find(red_money).send_keys(money)
        self.find_by_xpath('随意玩乐，我买单～').send_keys(remarks)
        self.find(cb_only_mic).click()
        self.find_by_xpath('发红包').click()
        return self

    def send_diamonds_red(self, mun, money, remarks):
        '''
        发钻石红包
        :param mun:红包个数
        :param money: 红包钱数
        :param remarks: 备注
        :return:
        '''
        red_money = (By.ID, 'et_red_money')
        cb_only_mic = (By.ID, 'cb_only_mic')
        self.find(self._img_menu).click()
        self.find_by_xpath('发红包').click()
        self.find_by_xpath('钻石红包').click()
        self.find_by_xpath('填写个数').send_keys(mun)
        self.find(red_money).send_keys(money)
        self.find_by_xpath('随意玩乐，我买单～').send_keys(remarks)
        self.find(cb_only_mic).click()
        self.find_by_xpath('发红包').click()
        return self

    def send_red_rain_success(self):
        '''
        判断发送红包雨是否成功
        :return:
        '''
        r = (By.XPATH,
             '//*[contains(@resource-id, "text_view") and contains(@text, "发起了红包雨")]'
             )
        try:
            self.find(r)
            return True
        except:
            return False

    def send_coin_diamonds_success(self):
        '''
        判断发送金币和钻石红包是否成功
        :return:
        '''
        #todo:需要更精确的判断发送是否成功
        r = (By.ID, 'layout_red_packet')
        try:
            self.find(r)
            return True
        except:
            return False











